#include "FigureDraw.h"

string& FigureDraw::getListObjects()
{
	return listObjects;
}

bool FigureDraw::save(char *fileName)
{
	//Mo file co ten fileName, neu chua co se tao 1 file mang ten fileName, neu co file roi thi se xoa du lieu cu trong file.
	ofstream saveFile(fileName, ios::trunc);
	if(!saveFile){
	}
	//Tao chuoi s1, s2 de tao file svg. Chen cac hinh dang xml trong listObjects vao file.
	string s1 = "<svg xmlns=\"http://www.w3.org/2000/svg\">";
	saveFile << s1;
	saveFile << listObjects;
	string s2 = "</svg>";
	saveFile << s2;

	//Tra ve, "thu tuc"
	return true;
}

bool FigureDraw::open(char* fileName)
{
	//Mo file co ten fileName
	ifstream openFile(fileName);
	if(!openFile){
		//cout << "Khong mo duoc file";
	}

	//Chuyen du lieu trong file sang chuoi fileContent
	string fileContent;
	string svg;
	while(!openFile.eof()){
		openFile >> svg;
		fileContent += " " + svg;
	}
	openFile.close();

	//Chay lai cac hinh trong chuoi fileContent
	//Xu ly chuoi fileContent de ve lai cac hinh tu file, code cung, chua co tinh linh dong

	//Xoa bot cac dong chua tag svg trong fileContent, mang tinh thu cong
	int pos = fileContent.find("<svg");
	int pos2 = fileContent.find("svg\">");
	fileContent.erase(pos, pos2 + 4);
	pos = fileContent.find("</svg>");
	fileContent.erase(pos, pos+5);
	fileContent.erase(0,1);

	//Neu van con "<", nghia la con tag -> con hinh -> tiep tuc xu ly
	while(fileContent.find_first_of("<") == 0){
		pos = fileContent.find_first_of(' ');
		//Neu tag do la line, lay chuoi line do ra, xu ly de lay cac thong tin can thiet
		if(fileContent.substr(1,pos-1) == "line"){
			string line = fileContent.substr(0, fileContent.find_first_of("/>")+2);
			fileContent.erase(0, fileContent.find_first_of("/>")+2);
			int x1; stringstream ssx1(line.substr(line.find("x1")+4, line.find_first_of(" ") - 2)); ssx1 >> x1;
			int y1; stringstream ssy1(line.substr(line.find("y1")+4, line.find_first_of(" ") - 2)); ssy1 >> y1;
			int x2; stringstream ssx2(line.substr(line.find("x2")+4, line.find_first_of(" ") - 2)); ssx2 >> x2;
			int y2; stringstream ssy2(line.substr(line.find("y2")+4, line.find_first_of(" ") - 2)); ssy2 >> y2;
			int e = line.find("stroke")+8; 
			int f = line.find_first_of(')');
			string stroke = line.substr(line.find("stroke")+8, line.find_first_of(')') - line.find("stroke") - 7);
			int strokeWidth; stringstream ssstrokewidth(line.substr(line.find("stroke-width")+14, line.find_first_of(" ") - 2)); ssstrokewidth >> strokeWidth;
			 
			//Ve lai line tren man hinh
			drawLine(x1,y1,x2,y2,stroke,strokeWidth);
		}
		pos = fileContent.find_first_of(' ');
		//Neu tag do la hinh chu nhat, lay chuoi hinh chu nhat do ra, xu ly de lay cac thong tin can thiet
		if(fileContent.substr(1,pos-1) == "rect"){
			string rect = fileContent.substr(0, fileContent.find_first_of("/>")+2);
			fileContent.erase(0, fileContent.find_first_of("/>")+2);
			int x; stringstream ssx(rect.substr(rect.find("x")+3, rect.find_first_of(" ") - 2)); ssx >> x;
			int y; stringstream ssy(rect.substr(rect.find("y")+3, rect.find_first_of(" ") - 2)); ssy >> y;
			int width; stringstream sswidth(rect.substr(rect.find("width")+7, rect.find_first_of(" ") - 2)); sswidth >> width;
			int height; stringstream ssheight(rect.substr(rect.find("height")+8, rect.find_first_of(" ") - 2)); ssheight >> height;
			string stroke = rect.substr(rect.find("stroke")+8, rect.find_first_of(')') - rect.find("stroke") - 7);
			int strokeWidth; stringstream ssstrokewidth(rect.substr(rect.find("stroke-width")+14, rect.find_first_of(" ") - 2)); ssstrokewidth >> strokeWidth;
			string fill = rect.substr(rect.find("fill")+6, rect.find_last_of("\"") - rect.find("fill") - 5);
			//Ve lai hinh chu nhat tren man hinh
			drawRectangle(x,y,width,height,stroke,strokeWidth, fill);
		}
		pos = fileContent.find_first_of(' ');
		//Neu tag do la hinh ellipse, lay chuoi ellipse do ra, xu ly de lay cac thong tin can thiet
		if(fileContent.substr(1,pos-1) == "ellipse"){
			string line = fileContent.substr(0, fileContent.find_first_of("/>")+2);
			fileContent.erase(0, fileContent.find_first_of("/>")+2);
			int cx; stringstream sscx(line.substr(line.find("cx")+4, line.find_first_of(" ") - 2)); sscx >> cx;
			int cy; stringstream sscy(line.substr(line.find("cy")+4, line.find_first_of(" ") - 2)); sscy >> cy;
			int rx; stringstream ssrx(line.substr(line.find("rx")+4, line.find_first_of(" ") - 2)); ssrx >> rx;
			int ry; stringstream ssry(line.substr(line.find("ry")+4, line.find_first_of(" ") - 2)); ssry >> ry;
			string stroke = line.substr(line.find("stroke")+8, line.find_first_of(')') - line.find("stroke") - 7);
			int strokeWidth; stringstream ssstrokewidth(line.substr(line.find("stroke-width")+14, line.find_first_of(" ") - 2)); ssstrokewidth >> strokeWidth;
			string fill = line.substr(line.find("fill")+6, line.find_last_of("\"") - line.find("fill") - 5);
			//Ve lai hinh ellipse tren man hinh
			drawEllipse(cx,cy,rx,ry,stroke,strokeWidth, fill);
		}
	}
	return true;
}

int FigureDraw::Parse(string &parseString)
{
	//Lay so tu chuoi rgb(,,). vi du rgb(255, 0, 0): goi ham lan dau->255, goi ham lan hai->0, goi ham lan ba->0
	int posComma = parseString.find(',');
	int posOpenBracket = parseString.find('(');
	int posCloseBracket = parseString.find(')');
	string result;
	if(posComma > 0){
		result = parseString.substr(posOpenBracket+1, posComma - posOpenBracket - 1);
		parseString.erase(posOpenBracket+1, posComma - posOpenBracket +1);
	}
	else{
		result = parseString.substr(posOpenBracket+1, posCloseBracket - posOpenBracket -1);
	}
	
	//Ky thuat chuyen chuoi than so dung stringstream trong sstream
	stringstream ss(result);
	int num;
	if((ss >> num).fail()){
	}
	return num;
}
